import {
  login
} from '@/api/user.js'
import router from '@/router/index'
const state = {
  userName: '',
  token: ''
}
const mutations = {
  SET_USER_NAME(state, name) {
    state.userName = name
  },
  SET_TOKEN(state,token){
    state.token = token
  },
  LoginOut(state) {
    state.username = {}
    state.token = ''
    router.push({
      name: 'Login',
      replace: true
    })
  }
}
const actions = {
  async LoginIn({commit}, loginInfo) {
    const res = await login(loginInfo)
    // commit('setUserInfo', res.data.user)
    console.log(res)
    commit('SET_TOKEN', res.access_toke)
    console.log(state.token)
    if (res.code === 0) {
      const redirect = router.history.current.query.redirect
      if (redirect) {
        router.push({
          path: redirect
        })
      } else {
        router.push({
          path: '/layouts'
        })
      }
    }
    router.push({
      path: '/home'
    })
  },
  // 设置name
  setUserName({ commit }, name) {
    commit('SET_USER_NAME', name)
  },
  setToken({ commit }, token) {
    commit('SET_TOKEN', token)
  }
}
export default {
  state,
  mutations,
  actions
}
